'''
# Coding:utf-8
# Project: iiop
# Author: rtf
# Time: 2021-03-24 18:35:36
# FileName: docx_text.py
# Software: PyCharm
'''

import docx


def get_text(filename, project_type):
    if project_type == "CONSULTING_DESIGN":
        data_list = [
            {"key": "reporting_unit", "value": "申报部门", "operate": "split"},
            {"key": "date", "value": "申报时间", "operate": "split"},
            {"key": "mostly_according", "value": "1.1主要依据", "operate": "index+1"},
            {"key": "necessity_analysis", "value": "1.2必要性分析", "operate": "index+1"},
            {"key": "benefit_analysis", "value": "1.3效益分析", "operate": "index+1"}
        ]
    elif project_type == "DATA_ENGINEERING_TEMPLATE":
        data_list = [
            {"key": "reporting_unit", "value": "申报部门", "operate": "split"},
            {"key": "date", "value": "申报时间", "operate": "split"},
            {"key": "mostly_according", "value": "1.1主要依据", "operate": "index+1"},
            {"key": "necessity_analysis", "value": "1.3必要性", "operate": "index+1"},
            {"key": "benefit_analysis", "value": "1.4效益分析", "operate": "index+1"}
        ]
    elif project_type == "BUSINESS_APPLICATION":
        data_list = [
            {"key": "reporting_unit", "value": "申报部门", "operate": "split"},
            {"key": "date", "value": "申报时间", "operate": "split"}
        ]
    else:
        data_list = [
            {"key": "reporting_unit", "value": "申报部门", "operate": "split"},
            {"key": "date", "value": "申报时间", "operate": "split"},
            {"key": "basic_situation", "value": "1 基本信息", "operate": "index+1"}
        ]
    data_dict = docx_process(filename, data_list)
    return data_dict


def copy_docx(filename, fs_type):
    data_list = None
    if fs_type == "NON_PURCHASE":
        data_list = [
            {"key": "project_name", "value": "项目名称", "operate": "split"},
            {"key": "reporting_unit", "value": "项目申报单位", "operate": "split"},
            {"key": "date", "value": "申报时间", "operate": "split"},
            {"key": "basic_situation", "value": "1 基本信息", "operate": "index+1"}
        ]
    else:
        pass
    data_dict = docx_process(filename, data_list)
    return data_dict


def docx_process(filename, data_list):
    doc = docx.Document(filename)
    full_text = []
    data_dict = dict()
    for i in doc.paragraphs:  # 迭代docx文档里面的每一个段落
        if i.text:
            full_text.append(i.text)  # 保存每一个段落的文本
    for d in data_list:
        __index = [i for i, x in enumerate(full_text) if x.find(d.get("value")) != -1][0]
        if d.get("operate") == "split":
            values = full_text[__index].split("：")
            value = values[1]
        elif d.get("operate") == "index+1":
            value = full_text[__index + 1]
        else:
            value = None

        data_dict[d.get("key")] = value
    return data_dict

'''
docx_path = "/home/rtf/Project/Jna/onlineoffice/app_data/doc/测试数据22_1617160450.docx"
doc = docx.Document(docx_path)
full_text = []
data_dict = dict()
for i in doc.paragraphs:  # 迭代docx文档里面的每一个段落
    if i.text:
        full_text.append(i.text)  # 保存每一个段落的文本
# print(full_text)

tables = doc.tables  # 获取文件中的表格集
table = tables[1]  # 获取文件中的第一个表格
for i in range(2, len(table.rows)):  # 从表格第二行开始循环读取表格数据
    # cell(i,0)表示第(i+1)行第1列数据，以此类推
    print(table.rows[i].cells[0].text, table.rows[i].cells[9].text)
doc.save(docx_path)
'''